{% macro make_plural(count, what) -%}
    {% set count = count|int %}
    {% if count == 0 %}
	no {{ what }}
    {% elif count == 1 %}
	1 {{ what }}
    {% else %}
	{{ count }} {{ what }}s
    {% endif %}
{%- endmacro %}

{# Static Bing map #}
{% macro map_image(lat, lon) -%}
    {% if lat and lon %}
	{% set coords = (lat ~ ',' ~ lon) | urlencode %}
	<p><img width="250" height="250" alt="[Bing Map]"
	src="http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/{{ coords }}/14?ms=250,250&pp={{ coords }};0&key=Aha1lOg_Dx1TU7quU-wNTgDN3K3fI9d4MYRgNGIIX1rQI7SBHs4iLB6LRnbKFN5c">
    {% endif %}
{%- endmacro %}

{# Show checkin/moveto links in venue header. #}
{% macro venue_cmds(venue, dist) -%}
    <div class="buttonbox">

	{% if not dist %}
	    {% set dist = 9999 %}
	{% endif %}

	<form style="margin:0; padding:0; display:inline !important;" action="/checkin" method="post">
	    <input type="hidden" name="vid" value="{{ venue.id|e }}">
	    <input type="hidden" name="dist" value="{{ dist|e }}">
	    <input class="formbutton" type="submit" value="checkin">
	</form>

	<a class="vbutton" href="/checkin_long?{{
	    { 
		'vid' : venue.id, 
		'vname' : venue.name,
		'dist' : dist,
	    } | urlparms
	}}">checkin with options</a>

	{% set location = venue.location %}
	{% if location %}
	    {% set lat = location.lat %}
	    {% set lng = location.lng %}
	    {% if lat and lng %}
		<a class="vbutton" href="/coords?{{
		    {
			'geolat' : lat,
			'geolong' : lng,
		    } | urlparms
		}}">move to</a>
	    {% endif %}
	{% endif %}

	<a class="vbutton" href="{{ venue.canonicalUrl|e }}">web</a>
    </div>
{%- endmacro %}

{# Format the address block of a venue. #}
{% macro addr_fmt(venue) -%}

    {% set location = venue.location %}
    {% if location %}

	{% if location.address %}
	    {{ location.address|e }}<br>
	{% endif %}

	{% if location.crossStreet %}
	    ({{ location.crossStreet|e }})<br>
	{% endif %}

	{% if location.city or location.state or location.postalCode or location.country %}
	    {{ location.city|e }}, {{ location.state|e }} {{ location.postalCode|e }} {{ location.country|e }}<br>
	{% endif %}

    {% endif %}

    {% set contact = venue.contact %}
    {% if contact %}

	{% set phone = contact.phone %}
	{% set formattedPhone = contact.formattedPhone %}

	{% if formattedPhone %}
	    {% set phoneStr = formattedPhone %}
	{% else %}
	    {% set phoneStr = phone|phonefmt %}
	{% endif %}
	{% if phone and phoneStr %}
	    <a href="tel:{{ phone|urlencode }}">{{ phoneStr|e }}</a><br>
	{% endif %}

	{{ twitter_fmt(contact.twitter) }}
    {% endif %}

{%- endmacro %}

{% macro twitter_fmt(username) -%}
    {% set twitter = username|wordchars %}
    {% if twitter %}
	<a href="http://mobile.twitter.com/{{ twitter|urlencode }}">@{{ twitter|e }}</a><br>
    {% endif %}
{%- endmacro %}

{% macro category_fmt(cat) -%}
    <p><img src="{{ cat.icon.prefix|e ~ 'bg_32' ~ cat.icon.suffix|e }}" class="lefticon">{{ cat.name|e }}
    <br style="clear:both">
{%- endmacro %}

{% macro name_fmt(user, button=true) -%}
    {%- if user -%}
	<a {% if button %}class="button"{% endif %} href="/user?userid={{ user.id|urlencode }}">{{ user.firstName|e }}{% if user.lastName %} {{ user.lastName|e }}{% endif %}</a>
    {%- endif %}
{%- endmacro %}

{% macro venue_checkin_fmt(checkin) -%}
    {% set user = checkin.user %}
    <p><img src="{{ user.photo.prefix|e ~ '32x32' ~ user.photo.suffix|e }}" class="usericon, lefticon" alt="">{{ name_fmt(user, button=false) }}
    {% if checkin.shout %}
	<br>"{{ checkin.shout|e }}"
    {% endif %}
    <br>{{ checkin.createdAt | fuzzydelta }}
    <br style="clear:both">
{%- endmacro %}

{% macro tips_fmt(tips) -%}
    {% if tips.count > 0 %}
	<p><b>Tips:</b>
	{% for grp in tips.groups %}
	    {% for tip in grp['items'] %} 
		{% set user = tip.user %}
		<p><img src="{{ user.photo.prefix|e ~ '32x32' ~ user.photo.suffix|e }}" class="usericon, lefticon" alt="">{{ name_fmt(user, button=false) }} says: 
		{{ tip.text|e }}<br>
		(Posted: {{ tip.createdAt | datefmt }})<br style="clear:both">
	    {% endfor %}
	{% endfor %}
    {% endif %}
{%- endmacro %}

{% macro special_fmt(special) -%}
    <table class="image, righticon">
	<caption style="caption-side: bottom">
	    {{ special.title | d('Special Offer') | e }}
	</caption>
	<tr>
	    <td>
		<img src="http://foursquare.com/img/specials/{{ special.icon | d('check-in') | urlencode }}.png" alt="">
	    </td>
	</tr>
    </table>

    {% set venue = special.venue %}
    {% if venue %}
	<p><a class="button" href="/venue?vid={{ venue.id|urlencode }}"><b>{{ venue.name|e }}</b></a>
	<br>{{ addr_fmt(venue) }}
    {% endif %}

    {% if special.message %}
	<br>Message: {{ special.message|e }}
    {% endif %}

    {% if special.description %}
	<br>Description: {{ special.description|e }}
    {% endif %}

    {% if special.finePrint %}
	<br>Fine print: {{ special.finePrint|e }}
    {% endif %}

    {% if special.unlocked %}
	<br>Unlocked: {{ special.unlocked|e }}
    {% endif %}

    {% if special.state %}
	<br>State: {{ special.state|e }}
    {% endif %}

    {% if special.progress %}
	<br>Progress: {{ special.progress|e }} {{ special.progressDescription|e }} of {{ special.target|e }}
    {% endif %}

    {% if special.detail %}
	<br>Detail: {{ special.detail|e }}
    {% endif %}

    <br style="clear:both">
{%- endmacro %}

{% macro specials_fmt(special_items, nearby=false) -%}
    {% if special_items and special_items['items'] %}
	<p><b>Specials{% if nearby %} nearby{% endif %}:</b>
	<ul class="vlist">
	    {% for special in special_items['items'] %}
		<li>{{ special_fmt(special) }}</li>
	    {% endfor %}
	</ul>
    {% endif %}
{%- endmacro %}

{% macro photo_fmt(photo, venue_id = none, checkin_id = none, post = none) -%}
    {% set photoparms = { 'photoid' : photo.id } %}
    {% if venue_id %}
	{% do photoparms.update({ 'venid' : venue_id }) %}
    {% else %}
	{% do photoparms.update({ 'chkid' : checkin_id }) %}
    {% endif %}
    <p>{{ name_fmt(photo.user) }}:<br>
    <a href="/photo?{{ photoparms|urlparms }}"><img src="{{ photo.prefix|e ~ '100x100' ~ photo.suffix|e }}"></a><br>

    {% if post %}
	{% if post.text %}
	    "{{ post.text|e }}"<br>
	{% endif %}
	{% if post.source %}
	    via <a href="{{ post.url|e }}">{{ post.source.name|e }}</a> <img src="{{ post.source.icon|e }}"><br>
	{% endif %}
    {% endif %}

    ({{ photo.createdAt|fuzzydelta }})<br>
{%- endmacro %}

{% macro comments_cmd(checkin) -%}
    {% set comments = checkin.comments|d({ 'count' : 0 }) %}
    {% set photos = checkin.photos|d({ 'count' : 0 }) %}
    <span class="buttonbox">
	<a class="vbutton" href="/comments?chkid={{ checkin.id|urlencode }}">
		{{ make_plural(comments.count, 'comment') }}, 
		{{ make_plural(photos.count, 'photo') }}
	</a>
    </span>
{%- endmacro %}

{% macro comment_fmt(comment, checkin) -%}
    <p>{{ name_fmt(comment.user) }}: {{ comment.text|e }} ({{ comment.createdAt|fuzzydelta }})<br>
    <a class="vbutton" href="/delcomment?chkid={{ checkin.id|urlencode }}&commid={{ comment.id|urlencode }}">delete</a><br>
{%- endmacro %}

{# Format a venue in the venue search page. #}
{% macro venue_fmt(venue, lat, lon) -%}
    {% call(cat) prim_category(venue) %}
	<img class="righticon" src="{{ cat.icon.prefix|e ~ 'bg_32' ~ cat.icon.suffix|e }}">
    {% endcall %}

    {% set location = venue.location %}
    {% set distcompass = { 'lat' : location.lat, 'lon' : location.lng } | distcompass(lat, lon) %}

    <a class="button" href="/venue?vid={{ venue.id|urlencode }}"><b>{{ venue.name|e }}</b></a>
    {{ venue_cmds(venue, distcompass.dist) }}<br>
    {{ addr_fmt(venue) }}
    {{ '(%.1f mi %s)' | format(distcompass.dist|float, distcompass.compass|e) }}
    <br style="clear:both">
{%- endmacro %}

{% macro history_checkin_fmt(checkin, lat, lon) -%}
    {% set venue = checkin.venue %}
    {% if venue %}
	{% call(cat) prim_category(venue) %}
	    <img class="righticon" src="{{ cat.icon.prefix|e ~ 'bg_32' ~ cat.icon.suffix|e }}">
	{% endcall %}

	{% if venue.id %}
	    {% set location = venue.location %}
	    {% set distcompass = { 'lat' : location.lat, 'lon' : location.lng } | distcompass(lat, lon) %}
	    <a class="button" href="/venue?vid={{ venue.id|urlencode }}"><b>{{ venue.name|e }}</b></a> {{ venue_cmds(venue, distcompass.dist) }}<br>{{ addr_fmt(venue) }}
	    {{ '(%.1f mi %s)' | format(distcompass.dist|float, distcompass.compass|e) }}<br>
	{% else %}
	    {# Orphaned venues will be missing the id field. #}
	    <b>{{ venue.name|e }}</b><br>
	{% endif %}

    {% else %}
	{% if checkin.location %}
	    <p>{{ checkin.location.name|e }} (venueless)<br>
	{% endif %}
    {% endif %}

    {% if checkin.shout %}
	"{{ checkin.shout|e }}"<br>
    {% endif %}

    {{ comments_cmd(checkin) }}<br>

    {{ checkin.createdAt|fuzzydelta }}<br>
{%- endmacro %}

{% macro prim_category(venue) -%}
    {% if venue.categories %}
	{% for cat in venue.categories if cat.primary %}
	    {{ caller(cat) }}
	{% endfor %}
    {% endif %}
{%- endmacro %}

{% macro find_notifs(notif, ntype) -%}
    {%- for n in notif if n.type|lower == ntype|lower -%}
	{{ caller(n.item) }}
    {%- endfor %}
{%- endmacro %}

{% macro find_post(posts, photoid) -%}
    {% for post in posts if post.photoId == photoid %}
	{{ caller(post) }}
    {% else %}
	{{ caller(none) }}
    {% endfor %}
{%- endmacro %}

{% macro checkin_badge_fmt(checkin) -%}
    {% set badges = checkin.badges %}
    {% if badges and badges['items'] is iterable %}
	{% for b in badges['items'] %}
	    {% set iconurl = '' %}
	    {% set iconurl = b.image.prefix ~ b.image.sizes[0] ~ b.image.name if b.image and b.image.sizes is iterable %}
	    <p><img src="{{ iconurl|e }}" class="lefticon">
	    {{ b.unlockMessage|e }}<br>
	    {{ b.description|e }}
	    <br style="clear:both">
	{% endfor %}
    {% endif %}
{%- endmacro %}

{# vim:set ft=htmldjango tw=0: #}
